package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseRecyclerAdapter;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.bus.AddShopEvent;
import de.greenrobot.event.EventBus;

/**
 * Created by xiong on 2015/11/26.
 */
public class IndexGridAdapter extends BaseRecyclerAdapter<GoodsItem> {


    private Context context;

    public IndexGridAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_square_goods_item, parent, false);
        return new IndexViewholder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, final GoodsItem data) {
        final IndexViewholder indexViewholder = (IndexViewholder) viewHolder;
//        Glide.with(context).load()
        indexViewholder.goodsName.setText(data.getTitle());
//        indexViewholder.goodsTotalName.setText(data.getTitle2());
        int currt = Integer.parseInt(data.getCanyurenshu());

        int total = Integer.parseInt(data.getZongrenshu());

        int radio = (currt * 100 / total);
        indexViewholder.goodsProgressBg.setMax(total);
        indexViewholder.goodsProgressBg.setProgress(currt);
        indexViewholder.goodsTotal.setText(radio + "%");
        indexViewholder.goodsAddCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] loaction = new int[2];
                indexViewholder.goodsImg.getLocationInWindow(loaction);
                AddShopEvent addShopEvent = new AddShopEvent();
                GoodsItem dataitem = new GoodsItem();
                dataitem = data;
                addShopEvent.item = dataitem;
                dataitem.count = 1;
                addShopEvent.drawable = indexViewholder.goodsImg.getDrawable();
                addShopEvent.loaction = loaction;
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(indexViewholder.goodsImg.getLayoutParams().width,
                        indexViewholder.goodsImg.getLayoutParams().height);
                addShopEvent.layoutParams = layoutParams;
                EventBus.getDefault().post(addShopEvent);
            }
        });
        indexViewholder.goodTenLabel.setVisibility(data.getYunjiage().equals("10") ? View.VISIBLE : View.GONE);
        Glide.with(context).load(data.getThumb()).placeholder(R.drawable.img_blank).into(indexViewholder.goodsImg);
    }

    class IndexViewholder extends Holder {

        @Bind(R.id.goods_img)
        ImageView goodsImg;
        @Bind(R.id.goods_name)
        TextView goodsName;
        @Bind(R.id.goods_total_name)
        TextView goodsTotalName;
        @Bind(R.id.goods_total)
        TextView goodsTotal;
        @Bind(R.id.goods_progress_bg)
        ProgressBar goodsProgressBg;

        @Bind(R.id.goods_add_cart)
        Button goodsAddCart;
        @Bind(R.id.goods_free_label)
        ImageView goodsFreeLabel;
        @Bind(R.id.good_ten_label)
        ImageView goodTenLabel;
        @Bind(R.id.limit_duobao_id)
        ImageView limitDuobaoId;
        @Bind(R.id.custom_top_left_icon)
        ImageView customTopLeftIcon;

        public IndexViewholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
